package com.example.home.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.fragment.adapter.BingnewlistApter;
import com.example.home.fragment.adapter.NewsFragAdapter;
import com.example.home.fragment.bean.NewsFragBean;
import com.example.home.fragment.bean.NewsListDataBean;
import com.example.home.fragment.bean.NewsTabBean;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.tabs.TabLayout;


public class NewsFragment extends Fragment {
    private View view;
    private ViewPager bannervp;
    private TabLayout tabNew;
    private RecyclerView rcyNew;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_news, container, false);

        initView();
        //轮播图
        getBannerData();
        //新闻列表
        getNewDate();
        bingnewlist(9);

        return view;
    }

    private void getNewDate() {
        OKHttpTool.doNetRequest("prod-api/press/category/list",
                OKHttpTool.GET,
                NewsTabBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewsTabBean newsFragBean = (NewsTabBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for (NewsTabBean.DataBean datum : newsFragBean.getData()) {
                                    TabLayout.Tab tab = tabNew.newTab();
                                    tab.setText(datum.getName());
                                    tabNew.addTab(tab);
                                }
                                sendnewlist(newsFragBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void sendnewlist(NewsTabBean newsFragBean) {
        tabNew.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String name = tab.getText().toString();
                if (newsFragBean.getData().get(position).getName().equals(name))
                    bingnewlist(newsFragBean.getData().get(position).getId());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void bingnewlist(Integer id) {
        OKHttpTool.doNetRequest("prod-api/press/press/list?type=" + id,
                OKHttpTool.GET,
                NewsListDataBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewsListDataBean newsFragBean = (NewsListDataBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                BingnewlistApter bingnewlistApter = new BingnewlistApter(NewsFragment.this.getContext(), newsFragBean.getRows());
                                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsFragment.this.getContext());
                                rcyNew.setAdapter(bingnewlistApter);
                                rcyNew.setLayoutManager(linearLayoutManager);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getBannerData() {
        OKHttpTool.doNetRequest("prod-api/press/press/list",
                OKHttpTool.GET,
                NewsFragBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewsFragBean newsFragBean = (NewsFragBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannerData(newsFragBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindBannerData(NewsFragBean newsFragBean) {
        NewsFragAdapter bannerAdapter = new NewsFragAdapter(newsFragBean.getRows(),NewsFragment.this.getContext());
        bannervp.setAdapter(bannerAdapter);
        mHandler.sendEmptyMessageDelayed(0, 3 * 1000);

    }

    private void initView() {
        bannervp = view.findViewById(R.id.bannervp);
        tabNew = view.findViewById(R.id.tab_new);
        rcyNew = view.findViewById(R.id.rcy_new);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = bannervp.getCurrentItem();
                index = (index + 1) % 3;
                bannervp.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
            }
        }
    };

}